package com.cssy.xxkj.logic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicWarehouseOtherinDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseOtherinItemDAO;
import com.cssy.xxkj.logic.dao.LogicWarehousePriceDAO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtherinDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtherinItemDO;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherInService;
import com.cssy.xxkj.logic.service.ILogicWarehouseInventoryService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("logicWareHouseOtherInService")
public class LogicWareHouseOtherInServiceImpl implements ILogicWareHouseOtherInService{

	@Resource(name = "logicWarehouseOtherinDAO")
	private LogicWarehouseOtherinDAO logicWarehouseOtherinDAO;
	
	@Resource(name = "logicWarehouseOtherinItemDAO")
	private LogicWarehouseOtherinItemDAO logicWarehouseOtherinItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWarehouseOtherinDO>  list= logicWarehouseOtherinDAO.find(scs);
		rs.setRecordCount(logicWarehouseOtherinDAO.count(scs));
		rs.setResults(list);
		return rs;
	}
	
	@Override
	public List<LogicWarehouseOtherinItemDO> findItemListByMap(Map<String, Object> paramMap)
			throws SyBaseException{
		return logicWarehouseOtherinItemDAO.findListByMap(paramMap);
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		LogicWarehouseOtherinDO basic = new LogicWarehouseOtherinDO();
		basic.setBizModifyDate(new Date());
		basic.setBizModifyName(user.getName());
		basic.setId(id);
		basic.setRemoved(1l);
		return logicWarehouseOtherinDAO.deleteBizRemoved(id);
	}

	@Override
	public Integer update(LogicWarehouseOtherinDO t, User user) throws SyBaseException {
		t.setBizModifyDate(new Date());
		t.setBizModifyName(user.getName());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return logicWarehouseOtherinDAO.updateLogicWarehouseOtherinDO(t);
	}

	@Override
	public LogicWarehouseOtherinDO save(LogicWarehouseOtherinDO t, User user)
			throws SyBaseException {
		Date date=new Date();
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(date);
		t.setBizVersion(1);
		logicWarehouseOtherinDAO.insertLogicWarehouseOtherinDO(t);
		return t;
	}

	@Override
	public LogicWarehouseOtherinDO findListById(String id) throws SyBaseException {
		return logicWarehouseOtherinDAO.findLogicWarehouseOtherinDOByPrimaryKey(id);
	}

	@Override
	public void coSave(LogicWarehouseOtherinDO curAP, HttpServletRequest request)
			throws SyBaseException, SyCodeException {
		try{
			String json = request.getParameter("addressInfos");
			List<LogicWarehouseOtherinItemDO> axxlist=JSONArray.parseArray(json, LogicWarehouseOtherinItemDO.class);
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				if(!curAP.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_FOUR)){
					String zd010OtherDate =request.getParameter("zd010OtherDate");
					curAP.setZd010OtherDate(DateUtils.stringToDate(zd010OtherDate));
					int rt=0;
					rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					logicWarehouseOtherinItemDAO.deleteByPid(curAP.getId());
					for (LogicWarehouseOtherinItemDO lii : axxlist){
						lii.setId(IDCodeTypeUtil.getId());
						lii.setZd001OtherInId(curAP.getId());
						logicWarehouseOtherinItemDAO.insertLogicWarehouseOtherinItemDO(lii);
					}
				}
				if (curAP.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_FOUR)){
					int rt=0;
					rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					LogicWarehouseOtherinDO loi = logicWarehouseOtherinDAO.findLogicWarehouseOtherinDOByPrimaryKey(curAP.getId());
					for (LogicWarehouseOtherinItemDO lii : axxlist){
						logicHsUtilService.processHsOtherInItem(loi, lii, SessionUtil.getUser(request));
					}
				}
			}else{
					curAP.setZd001InCode(syCodeService.getCode(BillTypeConstant.OTHERINWH,BillTypeConstant.OTHERINWH,SyCode.DATE_YYMM,5));
					String id = IDCodeTypeUtil.getId();
					curAP.setId(id);
					String zd010OtherDate =request.getParameter("zd010OtherDate");
					curAP.setZd010OtherDate(DateUtils.stringToDate(zd010OtherDate));
					LogicWarehouseOtherinDO cdo=this.save(curAP, SessionUtil.getUser(request));
					for (LogicWarehouseOtherinItemDO lii : axxlist){
						lii.setId(IDCodeTypeUtil.getId());
						lii.setZd001OtherInId(id);
						logicWarehouseOtherinItemDAO.insertLogicWarehouseOtherinItemDO(lii);
					}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return logicWarehouseOtherinDAO.deleteBizRemoved(id);
	}
}
